import frappe


@frappe.whitelist()
def get_doc_withmeta():   
    doc_with_meta = {"meta":{},"data":{}}  
    doc_with_meta["meta"]["main"] = []    
    doc_with_meta["data"]["main"] = []  
    doc_with_meta["data"]["main"].append({})
    doc = frappe.get_doc('Sales Order', 'SAL-ORD-2024-00003') 
    skip_field_types = ["Section Break","Column Break","Tab Break","update_auto_repeat_reference","Button","Table"]     
    pk_item = {"name":"PK","fieldname":"PK","label":"PK","fieldtype":"","options":""}
    doc_with_meta["meta"]["main"].append(pk_item)
    doc_with_meta["data"]["main"][0]["PK"] = "SAL-ORD-2024-00003"
    for item in doc.meta.fields:
        if item.fieldtype not in skip_field_types:
            new_item = {"name":item.name,"fieldname":item.fieldname,"label":item.label,"fieldtype":item.fieldtype,"options":item.options}
            doc_with_meta["meta"]["main"].append(new_item) 
            doc_with_meta["data"]["main"][0][item.fieldname] = getattr(doc,item.fieldname)
        if item.fieldtype == "Table":
            doc_with_meta["meta"][item.options] = []
            child_meta = frappe.get_meta(item.options)
            for sub_item in child_meta.fields:                
                if sub_item.fieldtype not in skip_field_types:
                    new_sub_item = {"name":sub_item.name,"fieldname":sub_item.fieldname,"label":sub_item.label,"fieldtype":sub_item.fieldtype,"options":sub_item.options}  
                    doc_with_meta["meta"][item.options].append(new_sub_item)                       
            doc_with_meta["data"][item.options]=getattr(doc,item.fieldname)            
    return doc_with_meta